/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.StReprografiaDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.StReprografia;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

/**
 *
 * @author CAR
 */
public class atualizaStReprografia extends HttpServlet {


     public atualizaStReprografia() {
        super();
    }

    private String storeId;
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //parse JSON object and populate the Customer bean 

        
        updateReprografia(request, response);
        
        //get the store Id from the request
//        storeId = request.getParameter("store_id");
        //get the request type update or insert
//        String action = request.getParameter("action");

        //if update
//        if (action.trim().equalsIgnoreCase("update")) {
//            updateDiaria(request, response);
//        } //if insert
//        else if (action.trim().equalsIgnoreCase("insert")) {
//            insertCustomer(request, response);
//        }
    }

    private void updateReprografia(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //get the record Information 
        String recordInfo = request.getParameter("recordInfo");

        //parse JSON object and populate the Customer bean 
        JSONObject reprografiaObj = (JSONObject) JSONSerializer.toJSON(recordInfo);
        StReprografia reprografia = (StReprografia) JSONObject.toBean(reprografiaObj, StReprografia.class);
        
        StReprografiaDAO reprografiaDAO = new StReprografiaDAO();
        
        float qntTotal = reprografia.getQntAlunos() * reprografia.getQntCopias();
        float valorFinal = qntTotal * reprografia.getVlrUnit();
        
        reprografia.setQntTotal(qntTotal);
        reprografia.setVlrFinal(valorFinal);
        
        boolean success = reprografiaDAO.updateReprografias(storeId, reprografia);

        PrintWriter out = response.getWriter();
        response.setContentType("text/html");

        //send response back whether the request was successful
        JSONObject myObj = new JSONObject();
        myObj.put("success", success);
        myObj.put("reprografias",reprografia);
        out.println(myObj.toString());
        out.close();

    }

}
        